
# def foo():
#     print("foo")
#
# f = foo
#
# f()
#
# f = lambda x : x + 1
#
# print(f(1))
#
# f = lambda x,y : x + y
#
# print(f(1,2))

# 通用型装饰器
def set_func(func):
    print("----a-----")
    def call_func(*args,**kwargs):
        print("----1----")
        return func(*args,**kwargs)
    return call_func

def set_func2(func):
    print("----b-----")
    def call_func(*args,**kwargs):
        print("----2----")
        return func(*args,**kwargs)
    return call_func

# @set_func # 等价于 test = set_func(test)
# def test():
#     print("----test-----")

# 先装2后装1，执行时先执行1后执行2
@set_func
@set_func2
def test1(a,b):
    print("-----test1----" + str(a))
    print("-----test1----" + str(b))
    return "ok","haha"

res = test1(123,b=456)
print(res)

##################################################
def set_func3(func):
    def call_func():
        return "<h1>" + func() + "</h1>"
    return call_func

@set_func3
def test3():
    return "ok"

print(test3())

#####################################################
class Test(object):
    def __init__(self,func):
        self.func = func

    def __call__(self, *args, **kwargs):
        print("-------test-------")
        return self.func()

@Test # 相当于 test4 = Test(test4)
def test4():
    return "ok"

print(test4())